﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DevComponents.DotNetBar;

namespace StuMana.UI
{
    public partial class Setting : Office2007Form
    {
        private Font fontForAll = null;
        private StyleManager managerStyle=new DevComponents.DotNetBar.StyleManager();

        public Setting()
        {
            InitializeComponent();

            int wPanel = this.gPanelMiddle.Size.Width;
            int hPanel = this.gPanelMiddle.Size.Height;

            int w2Panel = ((int)(wPanel / 2));
            int h2Panel = ((int)(hPanel / 2));

            int wPanel2 = this.panelExMiddle.Size.Width;
            int hPanel2 = this.panelExMiddle.Size.Height;

            int w2Panel2 = ((int)(wPanel2 / 2));
            int h2Panel2 = ((int)(hPanel2 / 2));

            int x = w2Panel - w2Panel2;
            int y = h2Panel - h2Panel2;

            this.panelExMiddle.Location = new Point(x, y);
        }

        public void ReadSetting()
        {
            XmlReader reader = XmlReader.Create("Settings.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                && reader.Name == "font")
                {
                    string name=reader.GetAttribute(0).ToString();
                    string style = reader.GetAttribute(1).ToString();
                    float size = float.Parse(reader.GetAttribute(2).ToString());

                    txtFontName.Text = name;
                    txtStyle.Text = style;
                    txtSize.Text = size.ToString();

                    if(style == "Bold")
                       fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(),size,System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Italic")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Regular")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(),size,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Strikeout")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Underline")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "managerstyle" && reader.NodeType == XmlNodeType.Element)
                        {
                            reader.Read();
                            string stylemana=reader.Value.ToString();

                            if(stylemana=="Office2007Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Blue;
                            if(stylemana=="Office2007Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Silver;
                            if(stylemana=="Office2007Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Black;
                            if(stylemana=="Windows7Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Windows7Blue;
                            if(stylemana=="Office2007VistaGlass")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007VistaGlass;
                            if(stylemana=="Office2010Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Silver;
                            if(stylemana=="Office2010Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Blue;
                            if(stylemana=="Office2010Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Black;

                           cmbChooseStyle.SelectedText = stylemana;
                        }                
                    }
                } 
            }
            reader.Close();
        }

        public void ConfigSetting()
        {
            ReadSetting();
            for(int i=0;i<Controls[0].Controls[0].Controls[0].Controls.Count;i++) 
            {
                if (Controls[0].Controls[0].Controls[0].Controls[i] is LabelX)
                {
                    Controls[0].Controls[0].Controls[0].Controls[i].Font = fontForAll;
                }
            }
            styleManager1 = managerStyle;            
        }

        private void Setting1_Load(object sender, EventArgs e)
        {
            ConfigSetting();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Write setting 
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create("Settings.xml", settings);
            writer.WriteStartDocument();
            writer.WriteComment("setting file for all form");
            writer.WriteStartElement("settings");

            writer.WriteStartElement("font");
            writer.WriteAttributeString("name", txtFontName.Text.ToString());
            writer.WriteAttributeString("style", txtStyle.Text.ToString());
            writer.WriteAttributeString("size", txtSize.Text);
            writer.WriteEndElement();

            writer.WriteStartElement("managerstyle");
            writer.WriteString(cmbChooseStyle.Text.ToString());
            writer.WriteEndElement();

            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();

            ConfigSetting();
            cmbChooseStyle.Text = styleManager1.ManagerStyle.ToString();
        }

        private void btnChooseFont_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FontDialog frmFont = new FontDialog();
            DialogResult result = frmFont.ShowDialog();

            if (result == DialogResult.OK)
            {
                Font font = frmFont.Font;
                txtFontName.Text = font.Name.ToString();
                txtStyle.Text = font.Style.ToString();
                txtSize.Text = font.Size.ToString();
            }
        }

        private void Setting_Resize(object sender, EventArgs e)
        {
            int wPanel = this.gPanelMiddle.Size.Width;
            int hPanel = this.gPanelMiddle.Size.Height;

            int w2Panel = ((int)(wPanel / 2));
            int h2Panel = ((int)(hPanel / 2));

            int wPanel2 = this.panelExMiddle.Size.Width;
            int hPanel2 = this.panelExMiddle.Size.Height;

            int w2Panel2 = ((int)(wPanel2 / 2));
            int h2Panel2 = ((int)(hPanel2 / 2));

            int x = w2Panel - w2Panel2;
            int y = h2Panel - h2Panel2;

            this.panelExMiddle.Location = new Point(x, y);
        }

        private void Setting_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
            Application.ExitThread();
            Application.Exit();
        }

        private void Setting_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
            Application.ExitThread();
            Application.Exit();
        }
    }
}
